
using UnityEngine;

namespace KunURP
{
    public static class Vector2Extension
    {
        // positive if v2 is on the left side of v1
        public static float SignedAngle(this Vector2 v1, Vector2 v2)
        {
            Vector2 n1 = v1.normalized;
            Vector2 n2 = v2.normalized;

            float dot = Vector2.Dot(n1, n2);
            if (dot > 1.0f)
                dot = 1.0f;
            if (dot < -1.0f)
                dot = -1.0f;
            
            float theta = Mathf.Acos(dot);
            float sgn = Vector2.Dot(new Vector2(-n1.y, n1.x), n2);
            if (sgn >= 0.0f)
                return theta;
            else
                return -theta;
        }

        public static Vector2 Rotate(this Vector2 v, float theta)
        {
            float cs = Mathf.Cos(theta);
            float sn = Mathf.Sin(theta);
            float x1 = v.x * cs - v.y * sn;
            float y1 = v.x * sn + v.y * cs;
            return new Vector2(x1, y1);
        }
    }

    public static class Vector3Extension
    {
        public static Vector3 WithX(this Vector3 v, float x)
        {
            return new Vector3(x, v.y, v.z);
        }

        public static Vector3 WithY(this Vector3 v, float y)
        {
            return new Vector3(v.x, y, v.z);
        }

        public static Vector3 WithZ(this Vector3 v, float z)
        {
            return new Vector3(v.x, v.y, z);
        }
    }
}